package com.evolution.model;

import com.evolution.utils.RandomGenerator;

public class Organism {

	private static final int MAX_AGE = 120;
	private static final int INITIAL_ENERGY = 100;
	private static final int MIN_BIRTHING_AGE = 18;
	private static final int MAX_BIRTHING_AGE = 85;
	private static final int BIRTHING_ENERGY_REQUIREMENT = 15;
	private static final int BIRTHING_PROBABILITY_SKEW = 10;
	
	private int ID;
	private int speciesID;
	
	private int age;
	private int size;
	private int energy;
	private int stingerSize;
	private Space space;
	private Organism parent;
	
	public Organism(Organism parent){
		this.parent = parent;
		age = 0;
		energy = INITIAL_ENERGY;
	}
	
	public Space getSpace(){ return space; }
	public void setSpace(Space space){ this.space = space; }
	public Organism getParent(){ return this.parent; }
	
	public int getID() {
		return ID;
	}

	public void setID(int iD) {
		ID = iD;
	}

	public int getSpeciesID() {
		return speciesID;
	}

	public void setSpeciesID(int speciesID) {
		this.speciesID = speciesID;
	}

	public void growOlder() {
		this.age++;
		if(age > MAX_AGE){
			die();
		}
	}
	
	public void consumeFood(Food food){
		this.energy = energy + food.getEnergyContent();
	}
	
	public void deployStinger(){
		int energyUsed = 10 * stingerSize;
		energy = energy - energyUsed;
		if(energy <= 0)
			die();
	}
	
	public void depleteEnergy(){
		energy = energy - size;
		if(energy <= 0)
			die();
	}
	
	public void reproduce(){
		if(age >= MIN_BIRTHING_AGE && age <= MAX_BIRTHING_AGE){
			if(energy >= BIRTHING_ENERGY_REQUIREMENT){
				if(RandomGenerator.randomBoolean(BIRTHING_PROBABILITY_SKEW)){
					OrganismUtils.birthNewOrganism(this);
				}
			}
		}
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getEnergy() {
		return energy;
	}

	public int getStingerSize() {
		return stingerSize;
	}

	public void setStingerSize(int stingerSize) {
		this.stingerSize = stingerSize;
	}
	
	public void die(){
		OrganismRegistry.getInstance().deregisterOrganism(this);
	}
	
}
